package com.tx.review.thread_.state;

/**
 *
 */
public class SleepDemo {
    public static void main(String[] args) throws InterruptedException {
        //测试sleep
        testSleep();
    }

    /**
     *  1、sleep可以不在同步块中
     *  2、sleep不会释放锁，但是会释放cpu资源（即可以让其他线程执行）
    */
    private static void testSleep() {
        ParkingLot parkingLot = new ParkingLot();
        new Thread(()-> {
            try {
                parkingLot.parking();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"A").start();

        new Thread(()-> {
            try {
                parkingLot.parking();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        },"B").start();
    }
}

class ParkingLot {
    public synchronized void parking() throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "-->停车");
        Thread.sleep(3000); //这时候不会释放锁，所以B线程也进不来
    }
}

